package com.example.sample;

import com.ohos.volley.toolbox.ImageLoader;
import ohos.media.image.PixelMap;
import ohos.utils.LruBuffer;

public class LruImageCache implements ImageLoader.ImageCache {

    private static LruBuffer<String, PixelMap> mMemoryCache;

    private static LruImageCache lruImageCache;

    private LruImageCache() {
        // Get the Max available memory
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cacheSize = maxMemory / 8;
        mMemoryCache = new LruBuffer<String, PixelMap>(cacheSize);
    }

    public static LruImageCache instance() {
        if (lruImageCache == null) {
            lruImageCache = new LruImageCache();
        }
        return lruImageCache;
    }

    @Override
    public PixelMap getBitmap(String arg0) {
        return mMemoryCache.get(arg0);
    }

    @Override
    public void putBitmap(String arg0, PixelMap arg1) {
        if (getBitmap(arg0) == null) {
            mMemoryCache.put(arg0, arg1);
        }
    }

}
